package com.iatinspur.dubbo.factory;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.service.GenericService;

import java.util.HashMap;
/**
 * 后面的测试 可以用这个
 */
public class GenericServiceFactory {
    /**
     * 获取nacos 中的dubbo接口
     * @param interfaceName 接口名
     * @param version       接口版本
     * @param protocol      duboo的协议
     * @return
     */
    public  static GenericService getNacosService(String interfaceName,String version,String protocol){
        ApplicationConfig config=new ApplicationConfig("consumer-do-provider");
        ReferenceConfig<GenericService> reference=new ReferenceConfig<>();
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("nacos://127.0.0.1:8848");
        registry.setUsername("nacos");
        registry.setPassword("nacos");
        HashMap<String,String> map=new HashMap<>();
        map.put("namespace", "d3883ff7-0010-456e-bae2-01e37fc114f4");
        map.put("group", "dubbo3.x");
        registry.setRegister(false);
        registry.setParameters(map);
        reference.setInterface(interfaceName);
        reference.setVersion(version);
        if(StringUtils.isNotBlank(protocol)){
            reference.setProtocol(protocol);
        }
        reference.setRegistry(registry);
        reference.setApplication(config);
        reference.setGeneric(true);
        reference.setAsync(true);
        return reference.get();
    }



    /**
     * 获取nacos 中的dubbo接口
     * @param interfaceName 接口名
     * @param version       接口版本
     * @param protocol      duboo的协议
     * @return
     */
    public  static GenericService getZookeeperService(String interfaceName,String version,String protocol){
        ApplicationConfig config=new ApplicationConfig("consumer-do-provider");
        ReferenceConfig<GenericService> reference=new ReferenceConfig<>();
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://127.0.0.1:2181");
        registry.setRegister(false);
        reference.setInterface(interfaceName);
        reference.setVersion(version);
        reference.setRegistry(registry);
        reference.setApplication(config);
        reference.setAsync(true);
        reference.setGeneric(true);
        if(StringUtils.isNotBlank(protocol)){
            reference.setProtocol(protocol);
        }
        return reference.get();
    }


    public  static GenericService getDubboService(String interfaceName,String version,String protocol,Integer port){
        ApplicationConfig config=new ApplicationConfig("consumer-do-provider");
        ReferenceConfig<GenericService> reference=new ReferenceConfig<>();
        reference.setInterface(interfaceName);
        reference.setVersion(version);
        reference.setUrl("dubbo://127.0.0.1:"+port);
        if(StringUtils.isNotBlank(protocol)){
            reference.setProtocol(protocol);
        }
        reference.setApplication(config);
        reference.setGeneric(true);

        return reference.get();
    }



}
